package com.xxx.yuedong.community.service.impl;

import cn.hutool.core.map.MapUtil;
import com.xxx.yuedong.common.R;
import com.xxx.yuedong.common.exception.YuedongException;
import com.xxx.yuedong.community.db.dao.AnnounceDao;
import com.xxx.yuedong.community.pojo.Announce;
import com.xxx.yuedong.community.service.AnnounceService;
import com.xxx.yuedong.mall.exception.MallException;
import com.xxx.yuedong.mall.exception.MallExceptionEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author HG
 * @version 1.0
 **/
@Service
@Slf4j
public class AnnounceServiceImpl implements AnnounceService {
    @Autowired
    private AnnounceDao announceDao;

    @Override
    public int insert(Announce announce) {
        int rows = announceDao.insert(announce);
        return rows;
    }

    @Override
    public int deleteById(Long[] ids) {
        for (Long id : ids) {
            if (announceDao.searchAnnounceById(id) == null) {
                throw new YuedongException(404, "公告不存在");
            }
        }

        int rows = announceDao.deleteById(ids);
        return rows;
    }

    @Override
    public HashMap searchAnnounceById(Long id) {
        HashMap announce = announceDao.searchAnnounceById(id);
        return announce;
    }

    @Override
    public R searchAllAnnounce(Integer page, Integer pageSize, String title) {
        int start = (page - 1) * pageSize;
        List<HashMap> list = announceDao.searchAllAnnounce(start, pageSize, title);
//        Integer totalCount = announceDao.searchAnnounceCount(start, pageSize, title);
        Integer totalCount = announceDao.selectCount(null);
        return R.ok().put("list",list).put("totalCount",totalCount);
    }
}
